package com.javaweb.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @desc: TODO: 使用注解 @NPECatch, 并使用@within, @annotation
 *            实现对类级别或方法级别的方法执行中的异常进行统一捕获处理。
 *            处理的逻辑为，输出在控制台。
 * @author:
 * @date:
 */
@Aspect
@Component
@Slf4j
public class NullExceptionAspect {
    @Pointcut("@within(com.javaweb.aop.NPECatch) || @annotation(com.javaweb.aop.NPECatch)")
    public void allCatchExceptionAnnotation() {
    }

    @Around("allCatchExceptionAnnotation()")
    public Object tryCatchException(ProceedingJoinPoint pjp) throws Throwable {
        log.info("INFO from allCatchExceptionAnnotation for method{}",pjp.getSignature().getName());
        Object result = null;
        try{
            result = pjp.proceed();
            log.info("INFO from allCatchExceptionAnnotation for method{} return {}",pjp.getSignature().getName(),result);
            return result;
        }catch (Exception e){
            log.error("INFO from allCatchExceptionAnnotation for method{} failed with Exception {}",pjp.getSignature().getName(),e.getMessage());
        }
        return null;
    }


}
